<template>
  <!-- 最外层的容器 -->
  <view class="my-checkout-container">
    <!-- 全选区域 -->
    <label class="radio" @click="$store.commit('cart/updateAllState', !isCheckedAll)">
      <radio color="#C00000" :checked="isCheckedAll" />
      <text>全选</text>
    </label>

    <!-- 合计区域 -->
    <view class="amount-box"> 合计:<text class="amount">￥{{checkedAmount}}</text> </view>

    <!-- 结算按钮 -->
    <view class="btn-checkout" @click="checkout">结算({{checkedTotal}})</view>
  </view>
</template>

<script>
import { mapGetters, mapState } from 'vuex'
export default {
  data() {
    return {
      seconds: 0,
      timerId: ''
    }
  },
  
  computed: {
    ...mapGetters('cart', ['checkedTotal', 'total', 'checkedAmount', 'fullAddress']),

    ...mapState('user', ['token']),
    ...mapState('cart', ['cartItems']),

    isCheckedAll() {
      return this.checkedTotal === this.total
    },
  },

  methods: {
    async checkout() {
      // 检查有无勾选商品
      if(this.checkedTotal === 0) return this.$msg('请勾选需要结算的商品')
      // 检查有无地址
      if(!this.fullAddress) return this.$msg('请选择收货地址')
      // 检查有无token
      if(!this.token) return this.countdown()

      // console.log('支付了')
      /* 
        微信支付流程:  => wx.requestPayment
          1. 请求接口 得到订单编号
          2. 请求接口 得到支付参数
          3. requestPayment 发起支付
          4. 请求接口 获取订单状态
      */
      const res = await this.$http.post('/my/orders/create', {
        order_price: 0.01, // 真实的接口是不能传支付金额(上课)
        consignee_addr: this.fullAddress,
        goods: this.cartItems.filter(item => item.goods_state).map(v => {
          return {
            goods_id: v.goods_id,
            goods_number: v.goods_count,
            goods_price: v.goods_price
          }
        })
      })
      console.log(res.message.order_number) // 订单编号

      const res2 = await this.$http.post('/my/orders/req_unifiedorder', {
        order_number: res.message.order_number
      })
      console.log(res2.message.pay) // 支付的参数

      const res3 = await uni.requestPayment(res2.message.pay)
      console.log(res3)

      const res4 = await this.$http.post('/my/orders/chkOrder', {
        order_number: res.message.order_number
      })
      console.log(res4)

      this.$msg('支付成功') // 跳转订单页面...
    },
    countdown() {
      // 完成3秒倒计时
      this.seconds = 3
      this.$msg(`${this.seconds}秒后跳转至登录`)

      this.timerId = setInterval(() => {
        this.seconds--

        if(this.seconds === 0) {
          // 跳转登录 + 清定时器
          uni.switchTab({
            url: '/pages/my/my',
          })

          clearInterval(this.timerId)
        }else{
          this.$msg(`${this.seconds}秒后跳转至登录`)
        }
      }, 1000)
    }
  }
}
</script>

<style lang="scss">
.my-checkout-container {
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 999;
  width: 100%;
  height: 100rpx;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 10rpx;
  font-size: 28rpx;

  .radio {
    display: flex;
    align-items: center;
  }

  .amount {
    color: #c00000;
  }

  .btn-checkout {
    height: 100rpx;
    min-width: 200rpx;
    background-color: #c00000;
    color: white;
    line-height: 100rpx;
    text-align: center;
    padding: 0 20rpx;
  }
}
</style>
